package com.wnzt.baszh.util.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.druid.sql.visitor.functions.If;

/**
 * 获取局域网ip
 */
public class GetLanIp {
	
	public static Object GetAllLanIp() {
			boolean flag = false;
			int count = 0;
			Map<String, String> map2 = new HashMap<String, String>();
			List<Map<String, String>> list = new ArrayList<Map<String,String>>();
			
			//添加本地的ip
			String localIpStr;
			try {
				localIpStr = InetAddress.getLocalHost().toString();
				map2.put("ip", localIpStr.split("/")[1]);
				list.add(map2);
			} catch (UnknownHostException e) {
				e.printStackTrace();
			}
			
			Runtime runtime = Runtime.getRuntime();
			Process process;
			try {
				process = runtime.exec("arp -a");
				BufferedReader bReader = new BufferedReader
						(new InputStreamReader(process.getInputStream()));
				String inline = "";
				while ((inline = bReader.readLine()) != null) {
					if (inline.indexOf("接口") > -1) {
						flag = !flag;
						if (!flag) {
							break;
						}
					}
					if (flag) {
						count ++;
						if (count > 2) {
							//有效ip
							String[] str=inline.split(" {4}");
							Map<String, String> map = new HashMap<String, String>();
							map.put("ip", str[0].trim());
							list.add(map);
							
						}
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			return list;
	}
}
